# -*- coding: utf-8 -*-

# 生成update_info.json文件，保存新版本的信息，放到服务器当中

import hashlib
import json
from version_information import VERSION, NAME, FILE_NAME, CURRENT_VERSION_NUMBER, DESCRIPTION, version_choice


def calculate_sha256(file_path):
    """计算文件的SHA256校验和"""
    sha256_hash = hashlib.sha256()
    with open(file_path, "rb") as file:
        # 读取文件块并更新散列对象
        for byte_block in iter(lambda: file.read(4096), b""):
            sha256_hash.update(byte_block)
    return sha256_hash.hexdigest()


file_path = "./dist/{}.exe".format(FILE_NAME)
json_file_path = "./VersionJson/update_information{}.json".format(VERSION)

name = NAME
version = CURRENT_VERSION_NUMBER
description = DESCRIPTION
if version_choice == 1 or version_choice == 3:
    download_url = "https://qn.aicache.wooxin.cn/client/beta/{}/UserCareSystem{}.exe".format(CURRENT_VERSION_NUMBER,
                                                                                             VERSION)
elif version_choice == 0 or version_choice == 2:
    download_url = "https://qn.aicache.wooxin.cn/client/release/{}/UserCareSystem{}.exe".format(CURRENT_VERSION_NUMBER,
                                                                                                VERSION)
# 计算新的哈希值
new_hash_value = calculate_sha256(file_path)
print(new_hash_value)

# 创建字典
data = {
    "name": name,
    "version": version,
    "description": DESCRIPTION,
    "download_url": download_url,
    "checksum": new_hash_value
}

# 转换为JSON格式
json_data = json.dumps(data, indent=4)

# 写入文件
with open(json_file_path, "w", encoding="utf-8") as json_file:
    json_file.write(json_data)
